﻿@page "/SysConfig"
@using SelfService.ViewModel.SysConfigVMs;
@using SelfService.Model
@inherits BasePage
@attribute [ActionDescription("配置管理", "SelfService.Controllers,SysConfig")]

<WTSearchPanel OnSearch="@DoSearch">
    <ValidateForm Model="@_searchModel">
        <Row ItemsPerRow="ItemsPerRow.Three" RowType="RowType.Inline">
            <BootstrapInput @bind-Value="@_searchModel.Key"  />
            <BootstrapInput @bind-Value="@_searchModel.Value"  />
            <Select @bind-Value="@_searchModel.GroupId" Items="@_allSysConfigGroups" PlaceHolder="@WtmBlazor.Localizer["Sys.All"]"></Select>
        </Row>
    </ValidateForm>
</WTSearchPanel>

<Table 
    @ref="_dataTable" 
    TItem="SysConfigView" 
    OnQueryAsync="OnSearch" 
    IsPagination="true" 
    IsStriped="true" 
    IsBordered="true"
    ShowRefresh="false"
    ShowToolbar="true" 
    IsMultipleSelect="true" 
    ShowExtendButtons="true" 
    ShowDefaultButtons="false" 
    style="margin-top:10px;">
    <TableColumns>
        <TableColumn @bind-Field="@context.Description"/>
        <TableColumn @bind-Field="@context.Key"/>
        <TableColumn @bind-Field="@context.Value">
            <Template Context="data">
                @if (data.Row.ValueType == SysConfigValueTypeEnum.文件)
                {
                    <Avatar @key="data.Value" Size="Size.ExtraSmall" GetUrlAsync="()=>WtmBlazor.GetBase64Image(data.Value.ToString(), 150, 150)" />
                }
                else
                {
                    <span>@data.Value</span>
                }
            </Template>
        </TableColumn>
        <TableColumn @bind-Field="@context.GroupName"/>
    </TableColumns>
    <TableToolbarTemplate>
        @if (IsAccessable("/api/SysConfig/Add"))
        {
            <TableToolbarButton TItem="SysConfigView" Color="Color.Primary" Icon="fa fa-fw fa-plus" Text="@WtmBlazor.Localizer["Sys.Create"]" OnClickCallback="OnCreateClick" />
        }
        @if (IsAccessable("/api/SysConfig/BatchDelete"))
        {
            <TableToolbarPopconfirmButton 
                TItem="SysConfigView" 
                Color="Color.Primary"
                Icon="fa fa-fw fa-trash-o" 
                Text="@WtmBlazor.Localizer["Sys.BatchDelete"]"
                OnConfirm="@OnBatchDeleteClick" 
                Content="@WtmBlazor.Localizer["Sys.BatchDeleteConfirm"]" 
                CloseButtonText="@WtmBlazor.Localizer["Sys.Close"]"
                ConfirmButtonText="@WtmBlazor.Localizer["Sys.BatchDelete"]" 
                ConfirmButtonColor="Color.Danger" />
        }
        @if (IsAccessable("/api/SysConfig/Import"))
        {
            <TableToolbarButton TItem="SysConfigView" Color="Color.Primary" Icon="fa fa-fw fa-upload" Text="@WtmBlazor.Localizer["Sys.Import"]" OnClickCallback="@OnImportClick" />
        }
        @if (IsAccessable("/api/SysConfig/ExportExcel"))
        {
            <TableToolbarButton TItem="SysConfigView" Color="Color.Primary" Icon="fa fa-fw fa-download" Text="@WtmBlazor.Localizer["Sys.Export"]" OnClickCallback="@OnExportClick" IsAsync="true" />
        }
    </TableToolbarTemplate>
    <RowButtonTemplate>
        <div style="padding-right:10px;">
            @if (IsAccessable("/api/SysConfig/Edit"))
            {
                <TableCellButton Size="Size.ExtraSmall" Color="Color.Success" Icon="fa fa-edit" Text="@WtmBlazor.Localizer["Sys.Edit"]" OnClick="() => OnEditClick(context)" />
            }
            @if (IsAccessable("/api/SysConfig/{id}"))
            {
                <TableCellButton Size="Size.ExtraSmall" Color="Color.Info" Icon="fa fa-info" Text="@WtmBlazor.Localizer["Sys.Details"]" OnClick="()=>OnDetailsClick(context)" />
            }
            @if (IsAccessable("/api/SysConfig/BatchDelete"))
            {
                <PopConfirmButton Icon="fa fa-trash-o" Text="@WtmBlazor.Localizer["Sys.Delete"]" OnConfirm="() => OnDeleteClick(context)" Color="Color.Danger" Size="Size.ExtraSmall"
                                  Content="@WtmBlazor.Localizer["Sys.DeleteConfirm"]" CloseButtonText="@WtmBlazor.Localizer["Sys.Close"]" ConfirmButtonText="@WtmBlazor.Localizer["Sys.Delete"]" ConfirmButtonColor="Color.Danger" />
            }
        </div>
    </RowButtonTemplate>
</Table>

@code{

    private readonly SysConfigSearcher _searchModel = new SysConfigSearcher();
    private Table<SysConfigView> _dataTable;

    private List<SelectedItem> _allSysConfigGroups = new List<SelectedItem>();


    protected override async Task OnInitializedAsync()
    {
        _allSysConfigGroups = await WtmBlazor.Api.CallItemsApi("/api/SysConfig/GetSysConfigGroups", placeholder: WtmBlazor.Localizer["Sys.All"]);
        await base.OnInitializedAsync();
    }

    private async Task<QueryData<SysConfigView>> OnSearch(QueryPageOptions opts)
    {
        return await StartSearch<SysConfigView>("/api/SysConfig/Search", _searchModel, opts);
    }

    private void DoSearch()
    {
        _dataTable.QueryAsync();
    }

    private async Task OnCreateClick(IEnumerable<SysConfigView> items)
    {
        if (await OpenDialog<Create>(WtmBlazor.Localizer["Sys.Create"]) == DialogResult.Yes)
        {
            await _dataTable.QueryAsync();
        }
    }

    private async Task OnEditClick(SysConfigView item)
    {
        if (await OpenDialog<Edit>(WtmBlazor.Localizer["Sys.Edit"], x => x.Id == item.ID.ToString()) == DialogResult.Yes)
        {
            await _dataTable.QueryAsync();
        }
    }

    private async Task OnDetailsClick(SysConfigView item)
    {
        await OpenDialog<Details>(WtmBlazor.Localizer["Sys.Details"], x => x.Id == item.ID.ToString());
    }

    private async Task OnBatchDeleteClick()
    {
        if (_dataTable.SelectedRows?.Any() == true)
        {
            await PostsData(_dataTable.SelectedRows.Select(x => x.ID).ToList(), $"/api/SysConfig/batchdelete", (s) => WtmBlazor.Localizer["Sys.BatchDeleteSuccess", s]);
            await _dataTable.QueryAsync();
        }
        else
        {
            await WtmBlazor.Toast.Information(WtmBlazor.Localizer["Sys.Info"], WtmBlazor.Localizer["Sys.SelectOneRowMin"]);
        }
    }

    private async Task OnDeleteClick(SysConfigView item)
    {
        await PostsData(new List<string> { item.ID.ToString() }, $"/api/SysConfig/batchdelete", (s) => "Sys.OprationSuccess");
        await _dataTable.QueryAsync();
    }


    private async Task OnExportClick(IEnumerable<SysConfigView> items)
    {
        if (_dataTable.SelectedRows?.Any() == true)
        {
            await Download("/api/SysConfig/ExportExcelByIds", _dataTable.SelectedRows.Select(x => x.ID.ToString()).ToList());
        }
        else
        {
            await Download("/api/SysConfig/ExportExcel", _searchModel);
        }
    }
    private async Task OnImportClick(IEnumerable<SysConfigView> items)
    {
        if (await OpenDialog<Import>(WtmBlazor.Localizer["Sys.Import"]) == DialogResult.Yes)
        {
            await _dataTable.QueryAsync();
        }
    }

}
